//
//  CZVerificationBtn.m
//  nsj_iOS
//
//  Created by apple on 2017/1/5.
//  Copyright © 2017年 GCZ. All rights reserved.
//

#import "CZVerificationBtn.h"
#import "CZLoginView.h"
@interface CZVerificationBtn ()
@property (nonatomic, strong) YYTimer *timer;
@end

@implementation CZVerificationBtn {
    CGFloat _timeout;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.layer.borderColor = ThemeColor.CGColor;
    self.layer.borderWidth = 1;
    self.layer.cornerRadius = 5;
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self setImageEdgeInsets:UIEdgeInsetsMake(2.5, -6, 2.5, 0)];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -8, 0, 0)];
    
    [self addTarget:self action:@selector(getVerifyicationCode) forControlEvents:(UIControlEventTouchUpInside)];
    _timeout = 60;
}

- (void)getVerifyicationCode {
    UIViewController *vc = (UIViewController *)self.delgate;
    [vc.view endEditing:YES];
    NSString *urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/user/smser",APP_HOST];
    if (!self.phoneNumber || self.phoneNumber.length != 11) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }
    self.enabled = NO;
    _timer = [YYTimer timerWithTimeInterval:1 target:self selector:@selector(countDown) repeats:YES];
    [GCZAFNetWorking POST:urlString params:@{@"phone" : self.phoneNumber} success:^(NSDictionary *response) {
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        
    }];
}
- (void)countDown {
    _timeout--;
    if (_timeout == 45) {
        if ([[self.delgate class] isSubclassOfClass:[CZUserLoginViewController class]]) {
            CZUserLoginViewController *loginVC = self.delgate;
            loginVC.loginView.voiceBtn.hidden = NO;
            [loginVC.loginView.voiceBtn addTarget:self action:@selector(alert) forControlEvents:(UIControlEventTouchUpInside)];
        } else {
            [self.delgate presentViewController:[self createAlert] animated:YES completion:nil];
        }
    }
    static NSString *btnTitle = @"";
    if (_timeout == 0) {
        [_timer invalidate];
        btnTitle = @"获取验证码";
        _timeout = 60;
        self.enabled = YES;
        [self setTitle:btnTitle forState:(UIControlStateNormal)];
        CZUserLoginViewController *loginVC = self.delgate;
        loginVC.loginView.voiceBtn.hidden = YES;
    } else {
        btnTitle = [NSString stringWithFormat: @"%.0f后可重发", _timeout];
        [self setTitle:btnTitle forState:(UIControlStateNormal)];
    }
}

- (void)alert {

    [self.delgate presentViewController:[self createAlert] animated:YES completion:nil];
}

- (UIAlertController *)createAlert {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"未收到验证码?" message:@"是否发送语音验证码" preferredStyle:(UIAlertControllerStyleAlert)];
    @weakify(self);
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [weak_self getVoiceVerificationCodeWithPhone];
        [alertVC dismissViewControllerAnimated:YES completion:nil];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        [alertVC dismissViewControllerAnimated:YES completion:nil];
    }];
    [alertVC addAction:cancelAction];
    [alertVC addAction:confirmAction];
    return alertVC;
}

#pragma mark- 获取语音验证码
- (void)getVoiceVerificationCodeWithPhone {
    NSString *urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/user/audio-smser",APP_HOST];
    
    [GCZAFNetWorking POST:urlString params:@{@"phone" : self.phoneNumber} success:^(NSDictionary *response) {
        [SVProgressHUD showSuccessWithStatus:@"语音验证码发送成功"];
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        
    }];
}

- (void)dealloc {
    [_timer invalidate];
    _timer = nil;
}

@end
